import Data from "../object/Data"
import ObservableArray from "../observer/observable.array"
import ObservableValue, { createObVal } from "../observer/observable.value"
import ObservableMap, { createMap } from "../observer/observable.map"
import mattertagEnum from "../enum/mattertag.enum"
import MattertagsData from "./MattertagsData"
export default class MattertagsViewData extends Data {
  mattertags: MattertagsData
  visitedTags: ObservableArray<any>
  navigatingTag: ObservableValue<any>
  toolStateObservable: ObservableValue<string>
  openTagObservable: ObservableValue<any>
  selectedTagObservable: ObservableValue<string>
  uncommittedTagObservable: ObservableValue<any>
  progressObservable: ObservableValue<number>
  screenPositionObservable: ObservableValue<any>
  viewedTags: number
  creatingNewMattertag: boolean
  tagCapabilities: Record<string, any>
  derivedData: any
  makeViewData: Function
  activeBillboards: ObservableMap<any>
  navigatingPosition: any
  constructor(mattertags: MattertagsData) {
    super()
    this.mattertags = mattertags
    this.name = "tag-view"
    this.visitedTags = new ObservableArray()
    this.navigatingTag = createObVal(null)
    this.toolStateObservable = createObVal(mattertagEnum.CLOSED)
    this.openTagObservable = createObVal(null)
    this.selectedTagObservable = createObVal(null)
    this.uncommittedTagObservable = createObVal(null)
    this.progressObservable = new ObservableValue(-1)
    this.screenPositionObservable = new ObservableValue(null)
    this.viewedTags = 0
    this.creatingNewMattertag = !1
    this.tagCapabilities = {}
    this.derivedData = {}
    this.activeBillboards = createMap()
  }
  getViewData(e) {
    const t = this.mattertags.getTag(e),
      i = this.derivedData[e]
    t && !i && this.makeViewData && (this.derivedData[e] = this.makeViewData(t))
    return this.derivedData[e]
  }
  setViewDataFactory(e) {
    this.makeViewData = e
  }
  openBillboard(e) {
    this.openTag && this.openTag !== e && this.closeBillboard(this.openTag)
    this.openTag !== e && this.setOpenTag(e)
  }
  hideBillboard(e) {
    this.openTag === e && this.setOpenTag(null)
  }
  closeBillboard(e) {
    this.openTag === e && this.setOpenTag(null)
    this.selectedTag === e && this.setSelectedTag(null)
  }
  closeOpenBillboard() {
    this.openTag && this.closeBillboard(this.openTag)
  }
  setTagCapabilities(e, t) {
    const i = this.tagCapabilities[e] || {}
    i.opening = t.opening
    i.navigating = t.navigating
    this.tagCapabilities[e] = i
    this.openTag !== e || this.tagCapabilities.opening || this.closeBillboard(e)
  }
  getTagCount() {
    return this.mattertags.collection.length
  }
  get navigating() {
    return this.navigatingTag.value
  }
  setNavigatingTag(e) {
    this.navigatingTag.value = e
  }
  onNavigatingTagChanged(e) {
    return this.navigatingTag.onChanged(e)
  }
  setSelectedTag(e: string) {
    this.selectedTagObservable.value = e
  }
  get selectedTag() {
    return this.selectedTagObservable.value
  }
  onSelectedTagChanged(e) {
    return this.selectedTagObservable.onChanged(e)
  }
  setUncommittedTag(e) {
    this.uncommittedTagObservable.value = e
  }
  get uncommittedTag() {
    return this.uncommittedTagObservable.value
  }
  onUncommittedChanged(e) {
    return this.uncommittedTagObservable.onChanged(e)
  }
  setProgress(e) {
    this.progressObservable.value = e
  }
  get progress() {
    return this.progressObservable.value
  }
  onProgressChanged(e) {
    return this.progressObservable.onChanged(e)
  }
  get screenPosition() {
    return this.screenPositionObservable.value
  }
  setScreenPosition(e) {
    this.screenPositionObservable.value = e
  }
  onScreenPositionChanged(e) {
    return this.screenPositionObservable.onChanged(e)
  }
  setToolState(e) {
    switch (e) {
      case mattertagEnum.CREATING:
        this.creatingNewMattertag = !0
        break
      case mattertagEnum.IDLE:
      case mattertagEnum.CLOSED:
        this.creatingNewMattertag = !1
    }
    this.toolStateObservable.value = e
  }
  get toolState() {
    return this.toolStateObservable.value
  }
  onToolStateChanged(e) {
    return this.toolStateObservable.onChanged(e)
  }
  get openTag() {
    return this.openTagObservable.value
  }
  setOpenTag(e) {
    this.openTagObservable.value = e
  }
  onOpenTagChanged(e: any) {
    return this.openTagObservable.onChanged(e)
  }
  isCreatingNewMattertag() {
    return this.creatingNewMattertag
  }
  visitTag(e: string) {
    this.viewedTags++, -1 === this.visitedTags.indexOf(e) && this.visitedTags.push(e)
  }
  getViewedTagCount() {
    return this.viewedTags
  }
  getVisitedTags() {
    return this.visitedTags.values()
  }
  setNavigatingPosition(e) {
    this.navigatingPosition = e
  }
  getNavigatingPosition() {
    return this.navigatingPosition
  }
}
